package com.shanzmoo.db.personnel.entity.vo;

import com.shanzmoo.base.exception.OutletOrderException;
import com.shanzmoo.db.personnel.entity.RootGroupEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单分配刷机组选择列表
 *
 * @author: Gzhao 2020/12/15
 */
@Data
@ApiModel("订单分配刷机组选择列表")
public class RootGroupSelectVo implements Serializable {

    @ApiModelProperty(value = "类型：0-自营，1-加盟，2-外包", hidden = true)
    private Integer type;

    @ApiModelProperty("类型：0-自营，1-加盟，2-外包")
    private String typeName;

    @ApiModelProperty("刷机组列表")
    private List<RootGroupInfoVo> rootGroupList;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor(access = AccessLevel.PRIVATE)
    public static class RootGroupInfoVo {

        @ApiModelProperty("刷机组id")
        private Integer rootGroupId;

        @ApiModelProperty("组名")
        private String name;

        @ApiModelProperty("负责人姓名")
        private String groupLeader;
    }

    public static List<RootGroupSelectVo> createList(Map<Integer,List<RootGroupEntity>> groupMap) {
        List<RootGroupSelectVo> relist = new ArrayList<>();

        for (Integer key :groupMap.keySet()){
            RootGroupSelectVo selectVo = new RootGroupSelectVo();
            selectVo.setType(key);
            selectVo.setTypeName(obtainTypeName(key));

            List<RootGroupInfoVo> rootGroupList = obtainRootGroupList(groupMap.get(key));
            selectVo.setRootGroupList(rootGroupList);

            relist.add(selectVo);
        }
        return relist;
    }

    private static List<RootGroupInfoVo> obtainRootGroupList(List<RootGroupEntity> rootGroupList) {
        return rootGroupList.stream().map(e->{
            return RootGroupInfoVo.builder()
                    .rootGroupId(e.getId())
                    .name(e.getName())
                    .groupLeader(e.getGroupLeader())
                    .build();
        }).collect(Collectors.toList());
    }

    private static String obtainTypeName(Integer key) {
        switch (key){
            case 0:
                return "自营";
            case 1:
                return "加盟";
            case 2:
                return "外包";
            default:
                throw new OutletOrderException("刷机组类型异常");
        }
    }
}
